import {reqBannerlist,reqNav,getTProductByLevel,getProductByType,updhot} from '../../api/index'
Page({
  data:{
    tproductList:[],//推荐套餐list
    otherTicketsList:[{
      englishstr:''
    }],//城外景点门票list
    chineseText: "",//中文字符串
    englishText: "",//英文字符串
    //轮播图素材
    swiperImg: [
      // 缺 少  素  材
    ],
    swiperIndex:0,//轮播图索引
    toolbar:[] //nav导航栏
  },
  //轮播图
 async bannerlist(){
    const banner=await reqBannerlist();
    this.setData({
      swiperImg:banner.data.list
    })
  },
  //nav导航栏
  async nav(){
    const nav=await reqNav();
    this.setData({
      toolbar:nav.data.nav
    })
  },
  //点击就跳转到买票的页面
  navbuyticket(e){
     let currentCate=e.currentTarget.dataset.index
     if(currentCate==3){
       currentCate=4
     }
    wx.reLaunch({
      url: '/pages/buyticket/buyticket?currentCate='+currentCate,
    })
  },
  
  navinf(){
      wx.navigateTo({
        url: '/pages/information/information?type=0',
      })
  },
  navrua(){
    wx.navigateTo({
        url: '/pages/Rural/Rural',
      })
  },

  onLoad(){
this.bannerlist()
this.nav()
    //推荐套餐查询
    this.getIndexData()
  },

  splitText: function(originalText) {
    const text = originalText;
    
    // 匹配中文：[\u4e00-\u9fa5] 是中文Unicode范围
    const chineseMatch = text.match(/[\u4e00-\u9fa5]+/g);
    // 匹配英文：[a-zA-Z]+ 是英文大小写字母范围
    const englishMatch = text.match(/[a-zA-Z]+/g);
    
    // 提取匹配到的文本，没有匹配到则为空字符串
    const chineseText = chineseMatch ? chineseMatch.join('') : '';
    const englishText = englishMatch ? englishMatch.join('') : '';
    
    // 更新数据
    this.setData({
      chineseText,
      englishText
    });
  },
  async getIndexData(){
    const data= await getTProductByLevel(2)
    console.log(data)
    
    const data2=await getProductByType()//城外景点门票
    console.log(data2)
    const list= data.data.list
    const list2=data2.data.productList
    for (let i = 0; i < list.length; i++) {
      if(list[i].name.length>12){
        list[i].name=list[i].name.substring(0,12)+'...'
      }
    }
    for (let i = 0; i < list2.length; i++) {
      const element = list2[i];
      this.splitText(element.productName)
      element.productName=this.data.chineseText
      element.englishName=this.data.englishText.length>22?this.data.englishText.substring(0,23)+"...":this.data.englishText
    }
    console.log(list2)
    this.setData({
      tproductList:list,
      otherTicketsList:list2
    })
  },
  //首页商品点击事件
  getProductInfo(e){
    console.log(e.currentTarget.dataset)
    const type=e.currentTarget.dataset.type
    const id=e.currentTarget.dataset.pid
    wx.navigateTo({
      url: '/pages/scenicdetails/scenicdetails?type='+type+'&id='+id,
    })
  },


})
